package org.flexlancer.CommentsLibrary
{
	import flash.events.ContextMenuEvent;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	
	import mx.containers.Canvas;
	import mx.containers.Panel;
	import mx.controls.Button;
	import mx.core.ScrollPolicy;
	
	import org.flexlancer.Logger.Logger;
	import org.flexlancer.events.CommentItemEvent;

	public class CommentObject extends Canvas
	{
		/*
		variables
		**/

		public var _output_panel:DraggablePanel;
		public var _btn_indicator:Button;
		[Bindable]
		private var cMenu:ContextMenu;
		
		[Embed(source="./assets/assets1.swf",symbol="CommentBubble")]
		[Bindable]
		private var _comment_image:Class;
		
		[Embed(source="./assets/assets1.swf",symbol="CommentBubbleSelected")]
		[Bindable]
		private var _comment_image_selected:Class;
		private var _last_mousex:Number=0;
		private var _last_mousey:Number=0;
		private var commentItems:Array = new Array();
		private var _user_name:String = "";
		public var prt_bound:Rectangle;
		public var straight:Boolean = true;
		
		public var CommentMove:Boolean = false;
		[Bindable]
		public var CommentName:String = "";
		
		private var _rotation:Number;
		private var __scale:Number = 1;
		
		/*
		Constructor
		**/
		public function CommentObject(parmUserName:String , 
														parmMessege:String,
														parmContainerBound:Rectangle,
														parmCommentName:String
														)
		{
			try{
				super();
				
				this._user_name = parmUserName;
				this.CommentName = parmCommentName;
				this.cMenu = new ContextMenu();
				this.cMenu.addEventListener(ContextMenuEvent.MENU_SELECT,OnMenuSelect);
				//--- panel;
				this._btn_indicator = new Button();
				this._btn_indicator.doubleClickEnabled = true;
				this._btn_indicator.setStyle("skin",this._comment_image);
				this._btn_indicator.label = this.CommentName;
				
				//this._btn_indicator.addEventListener(MouseEvent.MOUSE_DOWN,this.Indicator_OnMouseDown);
				//this._btn_indicator.addEventListener(MouseEvent.MOUSE_UP,this.Indicator_OnMouseUp);
				this.addEventListener(MouseEvent.MOUSE_DOWN,this.Indicator_OnMouseDown);
				this.addEventListener(MouseEvent.MOUSE_UP,this.Indicator_OnMouseUp);
				this._btn_indicator.addEventListener(MouseEvent.DOUBLE_CLICK,this.Indicator_OnDoubleClick);
				this._btn_indicator.addEventListener(MouseEvent.MOUSE_OVER,this.Indicator_OnMouseOver);
				this._btn_indicator.addEventListener(MouseEvent.MOUSE_OUT,this.Indicator_OnMouseOut);
				this._btn_indicator.contextMenu = this.cMenu;
				/* this._btn_indicator.width = 20;
				this._btn_indicator.height = 25; */
				this.addChild(this._btn_indicator);
				
				this._output_panel = new DraggablePanel();
				this._output_panel.addEventListener(MouseEvent.MOUSE_DOWN,OnPanelMouseDown);
				this._output_panel.addEventListener(MouseEvent.MOUSE_UP,OnPanelMouseUp);
				this._output_panel.addEventListener(MouseEvent.MOUSE_OVER,OnPanelMouseOver);
				this._output_panel.addEventListener(MouseEvent.MOUSE_OUT,OnPanelMouseOut);
			
				this._output_panel.title = "Sticky Notes ";
				
				this._output_panel.contextMenu = this.cMenu;
				this._output_panel.height = 305;
				this._output_panel.width = 227;
							
				this._output_panel.visible = false;
				
				
				this._output_panel.horizontalScrollPolicy = ScrollPolicy.OFF;
				// Bari ajeeb harkat karta he ye.
				//this._output_panel.verticalScrollPolicy = ScrollPolicy.ON;
				
				this.addChild(this._output_panel);
				
				if ( parmMessege != "" )
					this.AddComment(parmMessege,null);
				
				this.prt_bound = parmContainerBound;
				
				this.width = this._btn_indicator.width/2;
				this.height = this._btn_indicator.height/2;
				
				this.clipContent = false;
				
				this._btn_indicator.x = -this._btn_indicator.width/2; 
				this._btn_indicator.y = -this._btn_indicator.height/2;
				
				this._btn_indicator.scaleX = 2;
				this._btn_indicator.scaleY = 2;
				
				//this._output_panel.scaleX = 3.5;
				//this._output_panel.scaleY = 3.5;
				//this.x = this._btn_indicator.width/2;
				//this.y = this._btn_indicator.height/2;
				//this.x = 200;
				//this.y = 100;
				
				this.horizontalScrollPolicy = this.verticalScrollPolicy = ScrollPolicy.OFF;
				
				}catch(err:Error){
					throw err;
			}
		}
		/*
		Properties
		**/
		public function set Scale(val:Number):void{
			this.__scale = val;
			//this.scaleX = this.scaleY = val;
			this._btn_indicator.scaleX = this._btn_indicator.scaleY = 2 / val;
			this._output_panel.scaleX = this._output_panel.scaleY = 1 / val;
			this._output_panel.x = this._btn_indicator.x + this._btn_indicator.width + 5;
			this._output_panel.y = this._btn_indicator.y;
		}
		/*
		Implementation
		**/
		
		private function Indicator_OnMouseDown(evt:MouseEvent):void{
			try{
				stage.addEventListener( MouseEvent.MOUSE_UP, Indicator_OnMouseUp );
				this._last_mousex = this.x;
				this._last_mousey = this.y;
				var dragBound:Rectangle = new Rectangle(
					0,	0,
					this.prt_bound.width - this._btn_indicator.width,
					this.prt_bound.height - this._btn_indicator.height);
				this.startDrag(false,dragBound);
				this.CommentMove = true;
			}catch(err:Error){
				throw err;
			}
		}
		
		private function Indicator_OnMouseUp(evt:MouseEvent):void{
			try{
				if ( this._last_mousex == this.x &&
				this._last_mousey == this.y ){
					this.ShowPanel();
				}
				this.stopDrag();
				this.UpdateXY();
				this.CommentMove = false;
				stage.removeEventListener( MouseEvent.MOUSE_UP, Indicator_OnMouseUp );
			}catch(err:Error){
				throw err;
			}
		}
		
		/*
		private function Indicator_OnMouseDown(evt:MouseEvent):void{
			try{
				this._last_mousex = this._btn_indicator.x;
				this._last_mousey = this._btn_indicator.y;
				var dragBound:Rectangle = new Rectangle(this.prt_bound.x,this.prt_bound.y,this.prt_bound.width - this._btn_indicator.width,this.prt_bound.height - this._btn_indicator.height);
				this._btn_indicator.startDrag(false,dragBound);
				this.CommentMove = true;
			}catch(err:Error){
				throw err;
			}
		}
		
		private function Indicator_OnMouseUp(evt:MouseEvent):void{
			try{
				if ( this._last_mousex == this._btn_indicator.x &&
				this._last_mousey == this._btn_indicator.y ){
					this.ShowPanel();
				}
				this._btn_indicator.stopDrag();
				this.CommentMove = false;
			}catch(err:Error){
				throw err;
			}
		}
		*/
		
		private function Indicator_OnDoubleClick(evt:MouseEvent):void{
			try{
				this.HidePanel();
			}catch(err:Error){
				throw err;
			}
		}
		
		private function Indicator_OnMouseOver(evt:MouseEvent):void{
			try{
				this._btn_indicator.setStyle("skin",this._comment_image_selected);
				if ( this._output_panel != null ){
					this._output_panel.setStyle("borderColor",0x000000);	
				}
					
			}catch(err:Error){
				throw err;
			}
		}
		
		private function Indicator_OnMouseOut(evt:MouseEvent):void{
			try{
				this._btn_indicator.setStyle("skin",this._comment_image);
				if ( this._output_panel != null ){
					this._output_panel.setStyle("borderColor",0xffffff);
				}
					
			}catch(err:Error){
				throw err;
			}
		}
		
		private function ShowPanel():void{
			try{
				
				if(this._output_panel.visible == false){
					this._output_panel.x = this._btn_indicator.x + this._btn_indicator.width + 5;
					this._output_panel.y = this._btn_indicator.y;
					this._output_panel.visible = true;
				}
			}catch(err:Error){
				throw err;
			}
		}
		
		public function AddComment(parmMessege:String,parmDate:Date):void{
			try{
				var insert_comment:CommentItem = new CommentItem(this._user_name,parmMessege,parmDate);
				insert_comment.addEventListener(CommentItemEvent.REPLY_CLICK,OnReplyClicked);				
				this.commentItems.push(insert_comment);
				this._output_panel.addChild(insert_comment);
			}catch(err:Error){
				throw err;
			}
		}
				
		private function OnReplyClicked(evt:CommentItemEvent):void{
			try{
				this.AddComment("@"+evt.CommentUser+":",null);
				
			}catch(err:Error){
				throw err;
			}
		}
		
		 private function OnPanelMouseDown(event:MouseEvent):void{
			try{
				//Alert.show(event.target.toString()); //9
				if ( !(event.target is Panel)){//event.target is UITextField || event.target is ScrollThumb  ){
					event.stopImmediatePropagation();					
				}else{
					this.CommentMove = true;
				}
			}catch(err:Error){
				throw err;
			}
		} 
		
		private function OnPanelMouseUp(event:MouseEvent):void{
			try{
				this.CommentMove = false;
			}catch(err:Error){
				throw err;
			}
		}
		
		private function OnPanelMouseOver(event:MouseEvent):void{
			try{
				this._btn_indicator.setStyle("skin",this._comment_image_selected);
				if ( this._output_panel != null ){
					this._output_panel.setStyle("borderColor",0x000000);	
				}
					
			}catch(err:Error){
				throw err;
			}
		}
		
		private function OnPanelMouseOut(event:MouseEvent):void{
			try{
				this.CommentMove = false;
				this._btn_indicator.setStyle("skin",this._comment_image);
				if ( this._output_panel != null ){
					this._output_panel.setStyle("borderColor",0xffffff);
				}
					
			}catch(err:Error){
				throw err;
			}
		}
		
		private function HidePanel():void{
			try{
				this._output_panel.visible = false;
			}catch(err:Error){
				throw err;
			}
		}
		
		private function OnMenuSelect(evt:Event):void{
			try{
				var cItem:ContextMenuItem = new ContextMenuItem("Delete Comment Item",true);
				cItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,OnDeleteCommentItem);
				this.cMenu.hideBuiltInItems();
				this.cMenu.customItems = new Array();
				this.cMenu.customItems.push(cItem);
			}catch(err:Error){
				throw err;
			}
		}
		
		public function OnDeleteCommentItem(evt:ContextMenuEvent):void{
			try{
				var tempComment:CommentItem = this.commentItems.pop();
				if ( tempComment != null ){
					if (evt.currentTarget != "" ){
						var props:XML = new XML(<root />);
						props.CommentItem = tempComment.getXMLData();
						this.parentApplication.RecordHistory("ADDCOMMENTITEM",props);
					}
					this._output_panel.removeChild(tempComment);
				}
				if ( this.commentItems.length == 0 ){
					if ( this.parent != null ) 
						{	
							var props1:XML = new XML(<root />);
							props1.CommentObject = this.getXMLDatat();
							this.parentApplication.RecordHistory("ADDCOMMENTOBJECT",props1);
							this.parent.removeChild(this);
						}
				}
			}catch(err:Error){
				throw err;
			}
		}
		
		public function getXMLDatat():XML{
			try{
				var xml_data:XML = new XML(<commentobject />);
				xml_data.@x = this._btn_indicator.x;
				xml_data.@y = this._btn_indicator.y;
				xml_data.@username = this._user_name;
				xml_data.@commentname = this.CommentName;
				xml_data.@boundwidth = this.prt_bound.width;
				xml_data.@boundheight = this.prt_bound.height;
				for each ( var singleCommentItem:CommentItem in this.commentItems ){
					var retXML:XML = singleCommentItem.getXMLData();
					if ( retXML != null )
						xml_data.appendChild(retXML);
				}
				return xml_data;
			}catch(err:Error){
				throw err;
			}
			return xml_data;
		}
		
		private var _x:Number = 0;
		private var _y:Number = 0;
		
		private function ShiftXY():void{
			try{
				var tempWidth:Number;
				if(this.parent){
					 switch(this._rotation){
						case 0:
							super.x = this._x;
							super.y = this._y;
							if(this.straight){
								tempWidth = this.prt_bound.width;
								this.prt_bound.width = this.prt_bound.height;
								this.prt_bound.height = tempWidth;
								this.straight = false;	
							}else{
								tempWidth = this.prt_bound.width;
								this.prt_bound.width = this.prt_bound.height;
								this.prt_bound.height = tempWidth;
								this.straight = true;
							}
						 	break;
						case 90:
							super.x = CommentManager(this.parent).ContainerHeight - this._y;
							super.y = this._x;
							if(this.straight){
								tempWidth = this.prt_bound.width;
								this.prt_bound.width = this.prt_bound.height;
								this.prt_bound.height = tempWidth;
								this.straight = false;	
							}
							
							
							break;
						case 180:
							super.x = CommentManager(this.parent).ContainerWidth - this._x;
							super.y = CommentManager(this.parent).ContainerHeight - this._y;
							if(this.straight){
								tempWidth = this.prt_bound.width;
								this.prt_bound.width = this.prt_bound.height;
								this.prt_bound.height = tempWidth;
								this.straight = false;	
							}else{
								tempWidth = this.prt_bound.width;
								this.prt_bound.width = this.prt_bound.height;
								this.prt_bound.height = tempWidth;
								this.straight = true;
							}
							break;
						case 270:
							super.x = this._y;
							super.y = CommentManager(this.parent).ContainerWidth - this._x;
							if(this.straight){
								tempWidth = this.prt_bound.width;
								this.prt_bound.width = this.prt_bound.height;
								this.prt_bound.height = tempWidth;
								this.straight = false;	
							}
							break;
					}
				}
			}catch(err:Error){
				Logger.LogE(err);
			}
		}
		
		public function UpdateXY():void{
			try{
				 switch(this._rotation){
					case 0:
						this._x = super.x;
						this._y = super.y;
					 	break;
					case 90:
						this._x = CommentManager(this.parent).ContainerHeight - super.y;
						this._y = super.x;
						break;
					case 180:
						this._x = CommentManager(this.parent).ContainerWidth - super.x;
						this._y = CommentManager(this.parent).ContainerHeight - super.y;
						break;
					case 270:
						this._x = super.y;
						this._y = CommentManager(this.parent).ContainerWidth - super.x;
						break;
					default:
						this._x = super.x;
						this._y = super.y;
						break;
				}
				trace("UpdateXY("+this._x+","+this._y+")");
			}catch(err:Error){
				Logger.LogE(err);
			}
		}

		//---------------- Over ride properties
		
		override public function set rotation(value:Number):void{
			try{
				this._rotation = value;
				while ( this._rotation < 0 ) this._rotation += 360;
				this._rotation %= 360;
				super.rotation = 0;
				this.ShiftXY();
				trace("After rotation ("+this._x+","+this._y+")");
			}catch(err:Error){
				Logger.LogE(err);				
			}
		}
		
		override public function get rotation():Number{
			return this._rotation;
		}
		
		override public function set x(value:Number):void{
			super.x = value;
			this.UpdateXY();
		}
		
		override public function set y(value:Number):void{
			super.y = value;
			this.UpdateXY();
		}
	}//End of Class
}//End of Package